<?php

class News extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'tbl_comment':
	 * @var integer $id
	 * @var string $content
	 * @var integer $status
	 * @var integer $created
	 * @var string $author
	 * @var string $email
	 * @var string $url
	 * @var integer $post_id
	 */
	const STATUS_PENDING=1;
	const STATUS_APPROVED=2;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{news}}';
	}

	/**
	 * @return array validation rules for model attributes.

	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('content, author, email', 'required'),
			//array('author, email, url', 'length', 'max'=>128),
			//array('email','email'),
			//array('url','url'),
		);
	}
	 */
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)

	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'content' => 'Comment',
			'status' => 'Status',
			'create_time' => 'Create Time',
			'author' => 'Name',
			'email' => 'Email',
			'url' => 'Website',
			'page' => 'Page',
		);
	}
	 */

	/**
	 * @param integer the maximum number of comments that should be returned
	 * @return array the most recently added comments
	 */
	public function getAllNews($site)
	{
		$now = new CDbExpression('now()');
		return $this->findAll(array(
			'condition'=>'startdate <='.$now.' and enddate>'.$now. ' and (site=0 or site='.$site.')',
			'order'=>'startdate DESC',
		));
	}

	/**
	 * @param integer the maximum number of comments that should be returned
	 * @return array the most recently added comments
	 */
	public function getLastNews($site, $limit=3)
	{
		$now = new CDbExpression('now()');
		$criteria=new CDbCriteria(array(
			'order'=>'startdate DESC',
			'limit'=>$limit
		));
		return $this->findAll(array(
			'condition'=>'startdate <='.$now.' and enddate>'.$now. ' and (site=0 or site='.$site.')',
			'limit' =>$limit,
			'order'=>'startdate DESC',
		));
	}


}